package com.lizhi.yun.core.prescription;/**
 * @program: hbsf_yun
 * @name RszPrescriptionHandler
 * @description:
 * @author: lizhi
 * @create: 2021-11-17 08:58
 */

import com.lizhi.common.yun.entity.ListResponseResult;
import com.lizhi.mybatis.interceptor.PageBounds;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.rsz.dao.*;
import com.lizhi.yun.db.rsz.entity.*;
import com.lizhi.yun.db.rsz.enums.RszItemTypeEnum;
import com.lizhi.yun.deploy.NewBaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@program: hbsf_yun
 *@name RszPrescriptionHandler
 *@description:
 *@author: lizhi
 *@create: 2021-11-17 08:58
 */
@Slf4j
@Service
public class RszPrescriptionHandler extends NewBaseHandler {

    @Resource
    private RszPrescriptionDao rszPrescriptionDao;

    @Resource
    private RszPrescriptionItemDao rszPrescriptionItemDao;

    @Resource
    private RszPatientDao rszPatientDao;

    @Resource
    private RszTrajectoryInfoDao rszTrajectoryInfoDao;

    @Resource
    private RszTrajectorySelfDao rszTrajectorySelfDao;

    /**
     *功能描述 处方信息分页查询
     * @author lizhi
     * @date 2021/11/17
     * @param json:
     * @return com.lizhi.common.yun.entity.ListResponseResult
     */
    public ListResponseResult doMain(String json){
        Map<String, Object> map = SamJsonUtil.toMap(json);
        PageBounds pageBounds = getPageBounds(map);
        List<RszPrescriptionEntity> rszPrescriptionEntities = rszPrescriptionDao.selectByMap(map, pageBounds);
        for (RszPrescriptionEntity rszPrescriptionEntity : rszPrescriptionEntities){
            RszPatientEntity rszPatientEntity = rszPatientDao.selectById(rszPrescriptionEntity.ptId, rszPrescriptionEntity.hospitalId, rszPrescriptionEntity.hospitalSystemId);
            rszPrescriptionEntity.rszPatientEntity = rszPatientEntity;
            Map<String, Object> param = new HashMap<>();
            param.put("preId", rszPrescriptionEntity.preId);
            param.put("hospitalId", rszPrescriptionEntity.hospitalId);
            param.put("hospitalSystemId", rszPrescriptionEntity.hospitalSystemId);
            List<RszPrescriptionItemEntity> rszPrescriptionItemEntities = rszPrescriptionItemDao.selectByMap(param, null);
            rszPrescriptionEntity.rszPrescriptionItemEntitys = rszPrescriptionItemEntities;
            for (RszPrescriptionItemEntity rszPrescriptionItemEntity : rszPrescriptionItemEntities){
                if (RszItemTypeEnum.Item_Type_00.getEnValue().equals(rszPrescriptionItemEntity.itemType)){
                    RszTrajectoryInfoEntity rszTrajectoryInfoEntity = rszTrajectoryInfoDao.selectById(rszPrescriptionItemEntity.traId, rszPrescriptionItemEntity.hospitalId, rszPrescriptionItemEntity.hospitalSystemId);
                    rszPrescriptionItemEntity.trajectoryInfoEntity = rszTrajectoryInfoEntity;
                } else if (RszItemTypeEnum.Item_Type_01.getEnValue().equals(rszPrescriptionItemEntity.itemType)){
                    RszTrajectorySelfEntity rszTrajectorySelfEntity = rszTrajectorySelfDao.selectById(rszPrescriptionItemEntity.traId, rszPrescriptionItemEntity.hospitalId, rszPrescriptionItemEntity.hospitalSystemId);
                    rszPrescriptionItemEntity.trajectorySelfEntity = rszTrajectorySelfEntity;
                }
            }
        }
        return getListResult(pageBounds.getCountRows(), rszPrescriptionEntities, map);
    }
}
